home *** CD-ROM | disk | FTP | other *** search
/ Creating Your Own America Online Web Pages / Creating Your Own America Online Web Pages.iso / TOOLS / TEX2RTF / SOURCES.ZIP / SRC / TEX2RTF.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-28  |  21.2 KB  |  989 lines

  1. /*
  2.  * File:     tex2rtf.cc
  3.  * Purpose:  Converts Latex to linear/WinHelp RTF, HTML, wxHelp.
  4.  *
  5.  *                       wxWindows 1.50
  6.  * Copyright (c) 1993 Artificial Intelligence Applications Institute,
  7.  *                   The University of Edinburgh
  8.  *
  9.  *                     Author: Julian Smart
  10.  *                        Date: 7-9-93
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose is hereby granted without fee, provided
  14.  * that the above copyright notice, author statement and this permission
  15.  * notice appear in all copies of this software and related documentation.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  18.  * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  19.  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
  22.  * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
  23.  * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
  24.  * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
  25.  * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
  26.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <wx.h>
  30.  
  31. #ifndef NO_GUI
  32. #include <wx_help.h>
  33. #include <wx_timer.h>
  34. #endif
  35. #ifdef NO_GUI
  36. #include <iostream.h>
  37. #endif
  38.  
  39. #include <ctype.h>
  40. #include <stdlib.h>
  41. #include "tex2any.h"
  42. #include "tex2rtf.h"
  43. #include "rtfutils.h"
  44.  
  45. #if (defined(wx_x) && !defined(NO_GUI))
  46. #include "aiai.xbm"
  47. #endif
  48.  
  49. const float versionNo = 1.41;
  50.  
  51. TexChunk *currentMember = NULL;
  52. Bool startedSections = FALSE;
  53. char *contentsString = NULL;
  54. Bool suppressNameDecoration = FALSE;
  55. Bool OkToClose = TRUE;
  56. int passNumber = 1;
  57.  
  58. #ifndef NO_GUI
  59. wxHelpInstance *HelpInstance = NULL;
  60.  
  61. #ifdef wx_msw
  62. static char *ipc_buffer = NULL;
  63. static char Tex2RTFLastStatus[100];
  64. Tex2RTFServer *TheTex2RTFServer = NULL;
  65. #endif
  66. #endif
  67.  
  68. char *bulletFile = NULL;
  69.  
  70. FILE *Contents = NULL;   // Contents page
  71. FILE *Chapters = NULL;   // Chapters (WinHelp RTF) or rest of file (linear RTF)
  72. FILE *Sections = NULL;
  73. FILE *Subsections = NULL;
  74. FILE *Subsubsections = NULL;
  75. FILE *Popups = NULL;
  76.  
  77. char *InputFile = NULL;
  78. char *OutputFile = NULL;
  79. char *MacroFile = copystring("tex2rtf.ini");
  80.  
  81. char *FileRoot = NULL;
  82. char *ContentsName = NULL;    // Contents page from last time around
  83. char *TmpContentsName = NULL; // Current contents page
  84. char *RefName = NULL;         // Reference file name
  85.  
  86. char *RTFCharset = copystring("ansi");
  87.  
  88. #ifdef wx_msw
  89. int BufSize = 60;             // Size of buffer in K
  90. #else
  91. int BufSize = 500;
  92. #endif
  93.  
  94. Bool Go(void);
  95. void ShowOptions(void);
  96.  
  97. #ifdef NO_GUI
  98. int main(int argc, char **argv)
  99. #else
  100. wxMenuBar *menuBar = NULL;
  101. MyFrame *frame = NULL;
  102.  
  103. // This statement initializes the whole application and calls OnInit
  104. MyApp myApp;
  105.  
  106. // `Main program' equivalent, creating windows and returning main app frame
  107. wxFrame *MyApp::OnInit(void)
  108. #endif
  109. {
  110.   // Use default list of macros defined in tex2any.cc
  111.   DefineDefaultMacros();
  112.   AddMacroDef(ltHARDY, "hardy", 0);
  113.  
  114.   FileRoot = new char[300];
  115.   ContentsName = new char[300];
  116.   TmpContentsName = new char[300];
  117.   RefName = new char[300];
  118.  
  119.   int n = 1;
  120.   
  121.   // Read input/output files
  122.   if (argc > 1)
  123.   {
  124.     if (argv[1][0] != '-')
  125.     {
  126.       InputFile = argv[1];
  127.       n ++;
  128.  
  129.       if (argc > 2)
  130.       {
  131.         if (argv[2][0] != '-')
  132.         {
  133.           OutputFile = argv[2];
  134.           n ++;
  135.         }
  136.       }
  137.     }
  138.   }
  139.  
  140. #ifdef NO_GUI
  141.   if (!InputFile || !OutputFile)
  142.   {
  143.     cout << "Tex2RTF: input or output file is missing.\n";
  144.     ShowOptions();
  145.     exit(1);
  146.   }
  147. #endif
  148.   if (InputFile)
  149.   {
  150.     TexPathList.EnsureFileAccessible(InputFile);
  151.   }
  152.   if (!InputFile || !OutputFile)
  153.     isInteractive = TRUE;
  154.  
  155.   for (int i = n; i < argc;)
  156.   {
  157.     if (strcmp(argv[i], "-winhelp") == 0)
  158.     {
  159.       i ++;
  160.       convertMode = TEX_RTF;
  161.       winHelp = TRUE;
  162.     }
  163. #ifndef NO_GUI
  164.     else if (strcmp(argv[i], "-interactive") == 0)
  165.     {
  166.       i ++;
  167.       isInteractive = TRUE;
  168.     }
  169. #endif
  170.     else if (strcmp(argv[i], "-sync") == 0)  // Don't yield
  171.     {
  172.       i ++;
  173.       isSync = TRUE;
  174.     }
  175.     else if (strcmp(argv[i], "-rtf") == 0)
  176.     {
  177.       i ++;
  178.       convertMode = TEX_RTF;
  179.     }
  180.     else if (strcmp(argv[i], "-html") == 0)
  181.     {
  182.       i ++;
  183.       convertMode = TEX_HTML;
  184.     }
  185.     else if (strcmp(argv[i], "-xlp") == 0)
  186.     {
  187.       i ++;
  188.       convertMode = TEX_XLP;
  189.     }
  190.     else if (strcmp(argv[i], "-twice") == 0)
  191.     {
  192.       i ++;
  193.       runTwice = TRUE;
  194.     }
  195.     else if (strcmp(argv[i], "-macros") == 0)
  196.     {
  197.       i ++;
  198.       if (i < argc)
  199.       {
  200.         MacroFile = copystring(argv[i]);
  201.         i ++;
  202.       }
  203.     }
  204.     else if (strcmp(argv[i], "-bufsize") == 0)
  205.     {
  206.       i ++;
  207.       if (i < argc)
  208.       {
  209.         BufSize = atoi(argv[i]);
  210.         i ++;
  211.       }
  212.     }
  213.     else if (strcmp(argv[i], "-charset") == 0)
  214.     {
  215.       i ++;
  216.       if (i < argc)
  217.       {
  218.         char *s = argv[i];
  219.         i ++;
  220.         if (strcmp(s, "ansi") == 0 || strcmp(s, "pc") == 0 || strcmp(s, "mac") == 0 ||
  221.             strcmp(s, "pca") == 0)
  222.           RTFCharset = copystring(s);
  223.         else
  224.         {
  225.           OnError("Incorrect argument for -charset");
  226.         }
  227.       }
  228.     }
  229.     else
  230.     {
  231.       char buf[100];
  232.       sprintf(buf, "Invalid switch %s.\n", argv[i]);
  233.       OnError(buf);
  234.       i++;
  235. #ifdef NO_GUI
  236.       ShowOptions();
  237.       exit(1);
  238. #endif
  239.     }
  240.   }
  241.  
  242. #ifdef wx_msw
  243.   wxIPCInitialize();
  244.   Tex2RTFLastStatus[0] = 0; // DDE connection return value
  245.   TheTex2RTFServer = new Tex2RTFServer;
  246.   TheTex2RTFServer->Create("TEX2RTF");
  247. #endif
  248.  
  249. #ifdef wx_msw
  250.   // Limit to max Windows array size
  251.   if (BufSize > 64) BufSize = 64;
  252. #endif
  253.  
  254.   TexInitialize(BufSize);
  255.  
  256. #ifndef NO_GUI
  257.  
  258.   if (isInteractive)
  259.   {
  260.     char buf[100];
  261.  
  262.     // Create the main frame window
  263.     frame = new MyFrame(NULL, "Tex2RTF", 0, 0, 400, 300);
  264.     frame->CreateStatusLine(2);
  265.     // Give it an icon
  266. #ifdef wx_msw
  267.     wxIcon *icon = new wxIcon("tex2rtf");
  268. #endif
  269. #ifdef wx_x
  270.     wxIcon *icon = new wxIcon(aiai_bits, aiai_width, aiai_height);
  271. #endif
  272.  
  273.     frame->SetIcon(icon);
  274.  
  275.     if (InputFile)
  276.     {
  277.       sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(InputFile));
  278.       frame->SetTitle(buf);
  279.     }
  280.  
  281.     // Make a menubar
  282.     wxMenu *file_menu = new wxMenu;
  283.     file_menu->Append(TEX_GO, "&Go",                        "Run converter");
  284.     file_menu->Append(TEX_SET_INPUT, "Set &Input File",     "Set the LaTeX input file");
  285.     file_menu->Append(TEX_SET_OUTPUT, "Set &Output File",   "Set the output file");
  286.     file_menu->AppendSeparator();
  287.     file_menu->Append(TEX_VIEW_LATEX, "View &LaTeX File",   "View the LaTeX input file");
  288.     file_menu->Append(TEX_VIEW_OUTPUT, "View Output &File", "View output file");
  289.     file_menu->Append(TEX_SAVE_FILE, "&Save file", "Save current text into file");
  290.     file_menu->AppendSeparator();
  291.     file_menu->Append(TEX_QUIT, "E&xit",                    "Exit Tex2RTF");
  292.  
  293.     wxMenu *macro_menu = new wxMenu;
  294.  
  295.     macro_menu->Append(TEX_LOAD_CUSTOM_MACROS, "&Load Custom Macros", "Load custom LaTeX macro file");
  296.     macro_menu->Append(TEX_VIEW_CUSTOM_MACROS, "View &Custom Macros", "View custom LaTeX macros");
  297.  
  298.     wxMenu *mode_menu = new wxMenu;
  299.  
  300.     mode_menu->Append(TEX_MODE_RTF, "Output linear &RTF",   "Wordprocessor-compatible RTF");
  301.     mode_menu->Append(TEX_MODE_WINHELP, "Output &WinHelp RTF", "WinHelp-compatible RTF");
  302.     mode_menu->Append(TEX_MODE_HTML, "Output &HTML",        "HTML World Wide Web hypertext file");
  303.     mode_menu->Append(TEX_MODE_XLP, "Output &XLP",          "wxHelp hypertext help file");
  304.  
  305.     wxMenu *help_menu = new wxMenu;
  306.  
  307.     help_menu->Append(TEX_HELP, "&Help", "Tex2RTF Contents Page");
  308.     help_menu->Append(TEX_ABOUT, "&About Tex2RTF", "About Tex2RTF");
  309.  
  310.     menuBar = new wxMenuBar;
  311.     menuBar->Append(file_menu, "&File");
  312.     menuBar->Append(macro_menu, "&Macros");
  313.     menuBar->Append(mode_menu, "&Conversion Mode");
  314.     menuBar->Append(help_menu, "&Help");
  315.  
  316.     frame->SetMenuBar(menuBar);
  317.     frame->textWindow = new wxTextWindow(frame);
  318.  
  319.     (*frame->textWindow) << "Welcome to Julian Smart's LaTeX to RTF converter.\n";
  320. //    ShowOptions();    
  321.  
  322.     HelpInstance = new wxHelpInstance(TRUE);
  323.     HelpInstance->Initialize("tex2rtf");
  324.  
  325.     /*
  326.      * Read macro/initialisation file
  327.      *
  328.      */
  329.    
  330.     char *path;
  331.     if (path = TexPathList.FindValidPath(MacroFile))
  332.       ReadCustomMacros(path);
  333.  
  334.     strcpy(buf, "In ");
  335.  
  336.     if (winHelp && (convertMode == TEX_RTF))
  337.       strcat(buf, "WinHelp RTF");
  338.     else if (!winHelp && (convertMode == TEX_RTF))
  339.       strcat(buf, "linear RTF");
  340.     else if (convertMode == TEX_HTML) strcat(buf, "HTML");
  341.     else if (convertMode == TEX_XLP) strcat(buf, "XLP");
  342.       strcat(buf, " mode.");
  343.     frame->SetStatusText(buf, 1);
  344.   
  345.     frame->Show(TRUE);
  346.     return frame;
  347.   }
  348.   else
  349. #endif // NO_GUI
  350.   {
  351.     /*
  352.      * Read macro/initialisation file
  353.      *
  354.      */
  355.    
  356.     char *path;
  357.     if (path = TexPathList.FindValidPath(MacroFile))
  358.       ReadCustomMacros(path);
  359.  
  360.     Go();
  361.     if (runTwice) Go();
  362. #ifdef NO_GUI
  363.     return 0;
  364. #else
  365.     return NULL;
  366. #endif
  367.   }
  368.  
  369. #ifndef NO_GUI
  370.   // Return the main frame window
  371.   return frame;
  372. #else
  373.   return 0;
  374. #endif
  375. }
  376.  
  377. void ShowOptions(void)
  378. {
  379.     OnInform("Usage: tex2rtf [input] [output] [switches]\n");
  380.     OnInform("where valid switches are");
  381.     OnInform("    -interactive");
  382.     OnInform("    -bufsize <size in K>");
  383.     OnInform("    -charset <pc | pca | ansi | mac> (default ansi)");
  384.     OnInform("    -twice");
  385.     OnInform("    -sync");
  386.     OnInform("    -macros <filename>");
  387.     OnInform("    -winhelp");
  388.     OnInform("    -rtf");
  389.     OnInform("    -html");
  390.     OnInform("    -xlp\n");
  391. }
  392.  
  393. #ifndef NO_GUI
  394. // My frame constructor
  395. MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
  396.   wxFrame(frame, title, x, y, w, h)
  397. {}
  398.  
  399. Bool MyFrame::OnClose(void)
  400. {
  401.   if (!stopRunning && !OkToClose)
  402.   {
  403.     stopRunning = TRUE;
  404.     runTwice = FALSE;
  405.     return FALSE;
  406.   }
  407.   else if (OkToClose)
  408.   {
  409. #ifdef wx_msw
  410.     delete TheTex2RTFServer;
  411.     wxIPCCleanUp();
  412. #endif
  413.     return TRUE;
  414.   }
  415.   return FALSE;
  416. }
  417.  
  418. // Intercept menu commands
  419. void MyFrame::OnMenuCommand(int id)
  420. {
  421.   switch (id) {
  422.     case TEX_QUIT:
  423.       delete this;
  424.       break;
  425.     case TEX_GO:
  426.     {
  427.       menuBar->EnableTop(0, FALSE);
  428.       menuBar->EnableTop(1, FALSE);
  429.       menuBar->EnableTop(2, FALSE);
  430.       menuBar->EnableTop(3, FALSE);
  431.       textWindow->Clear();
  432.       Tex2RTFYield(TRUE);
  433.       Go();
  434.  
  435.       if (runTwice)
  436.       {
  437.         Tex2RTFYield(TRUE);
  438.         Go();
  439.       }
  440.       menuBar->EnableTop(0, TRUE);
  441.       menuBar->EnableTop(1, TRUE);
  442.       menuBar->EnableTop(2, TRUE);
  443.       menuBar->EnableTop(3, TRUE);
  444.       break;
  445.     }
  446.     case TEX_SET_INPUT:
  447.     {
  448.       ChooseInputFile(TRUE);
  449.       break;
  450.     }
  451.     case TEX_SET_OUTPUT:
  452.     {
  453.       ChooseOutputFile(TRUE);
  454.       break;
  455.     }
  456.     case TEX_SAVE_FILE:
  457.     {
  458.       char *s = wxFileSelector("Save text to file", NULL, NULL, "txt", "*.txt");
  459.       if (s)
  460.       {
  461.         textWindow->SaveFile(s);
  462.         char buf[350];
  463.         sprintf(buf, "Saved text to %s", s);
  464.         frame->SetStatusText(buf, 0);
  465.       }
  466.       break;
  467.     }
  468.     case TEX_VIEW_OUTPUT:
  469.     {
  470.       ChooseOutputFile();
  471.       if (OutputFile && FileExists(OutputFile))
  472.       {
  473.         textWindow->LoadFile(OutputFile);
  474.         char buf[300];
  475.         sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(OutputFile));
  476.         frame->SetTitle(buf);
  477.       }
  478.       break;
  479.     }
  480.     case TEX_VIEW_LATEX:
  481.     {
  482.       ChooseInputFile();
  483.       if (InputFile && FileExists(InputFile))
  484.       {
  485.         textWindow->LoadFile(InputFile);
  486.         char buf[300];
  487.         sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(InputFile));
  488.         frame->SetTitle(buf);
  489.       }
  490.       break;
  491.     }
  492.     case TEX_LOAD_CUSTOM_MACROS:
  493.     {
  494.       textWindow->Clear();
  495.       char *s = wxFileSelector("Choose custom macro file", PathOnly(MacroFile), FileNameFromPath(MacroFile), "ini", "*.ini");
  496.       if (s)
  497.       {
  498.         MacroFile = copystring(s);
  499.         ReadCustomMacros(s);
  500.         ShowCustomMacros();
  501.       }
  502.       break;
  503.     }
  504.     case TEX_VIEW_CUSTOM_MACROS:
  505.     {
  506.       textWindow->Clear();
  507.       Tex2RTFYield(TRUE);
  508.       ShowCustomMacros();
  509.       break;
  510.     }
  511.     case TEX_MODE_RTF:
  512.     {
  513.       convertMode = TEX_RTF;
  514.       winHelp = FALSE;
  515.       InputFile = NULL;
  516.       OutputFile = NULL;
  517.       SetStatusText("In linear RTF mode.", 1);
  518.       break;
  519.     }
  520.     case TEX_MODE_WINHELP:
  521.     {
  522.       convertMode = TEX_RTF;
  523.       winHelp = TRUE;
  524.       InputFile = NULL;
  525.       OutputFile = NULL;
  526.       SetStatusText("In WinHelp RTF mode.", 1);
  527.       break;
  528.     }
  529.     case TEX_MODE_XLP:
  530.     {
  531.       convertMode = TEX_XLP;
  532.       InputFile = NULL;
  533.       OutputFile = NULL;
  534.       SetStatusText("In XLP mode.", 1);
  535.       break;
  536.     }
  537.     case TEX_MODE_HTML:
  538.     {
  539.       convertMode = TEX_HTML;
  540.       InputFile = NULL;
  541.       OutputFile = NULL;
  542.       SetStatusText("In HTML mode.", 1);
  543.       break;
  544.     }
  545.     case TEX_HELP:
  546.     {
  547.       HelpInstance->LoadFile();
  548.       HelpInstance->DisplayContents();
  549.       break;
  550.     }
  551.     case TEX_ABOUT:
  552.     {
  553.       char buf[300];
  554.       sprintf(buf, "Tex2RTF Version %.2f\nLaTeX to RTF, WinHelp, HTML and wxHelp Conversion\n\n(c) Julian Smart 1994", versionNo);
  555.       wxMessageBox(buf, "About Tex2RTF");
  556.       break;
  557.     }
  558.   }
  559. }
  560.  
  561. void ChooseInputFile(Bool force)
  562. {
  563.   if (force || !InputFile)
  564.   {
  565.     char *s = wxFileSelector("Choose LaTeX input file", PathOnly(InputFile), FileNameFromPath(InputFile), "tex", "*.tex");
  566.     if (s)
  567.     {
  568.       // Different file, so clear index entries.
  569.       ClearKeyWordTable();
  570.       passNumber = 1;
  571.       char buf[300];
  572.       InputFile = copystring(s);
  573.       sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(InputFile));
  574.       frame->SetTitle(buf);
  575.       OutputFile = NULL;
  576.     }
  577.   }
  578. }
  579.  
  580. void ChooseOutputFile(Bool force)
  581. {
  582.   char extensionBuf[10];
  583.   char wildBuf[10];
  584.   strcpy(wildBuf, "*.");
  585.   char *path = NULL;
  586.   if (OutputFile)
  587.     path = PathOnly(OutputFile);
  588.   else if (InputFile)
  589.     path = PathOnly(InputFile);
  590.     
  591.   switch (convertMode)
  592.   {
  593.     case TEX_RTF:
  594.     {
  595.       strcpy(extensionBuf, "rtf");
  596.       strcat(wildBuf, "rtf");
  597.       break;
  598.     }
  599.     case TEX_XLP:
  600.     {
  601.       strcpy(extensionBuf, "xlp");
  602.       strcat(wildBuf, "xlp");
  603.       break;
  604.     }
  605.     case TEX_HTML:
  606.     {
  607. #ifdef wx_msw
  608.       strcpy(extensionBuf, "htm");
  609.       strcat(wildBuf, "htm");
  610. #else
  611.       strcpy(extensionBuf, "html");
  612.       strcat(wildBuf, "html");
  613. #endif
  614.       break;
  615.     }
  616.   }
  617.   if (force || !OutputFile)
  618.   {
  619.     char *s = wxFileSelector("Choose output file", path, FileNameFromPath(OutputFile),
  620.                    extensionBuf, wildBuf);
  621.     if (s)
  622.       OutputFile = copystring(s);
  623.   }
  624. }
  625. #endif
  626.  
  627. Bool Go(void)
  628. {
  629. #ifndef NO_GUI
  630.   ChooseInputFile();
  631.   ChooseOutputFile();
  632. #endif
  633.  
  634.   if (!InputFile || !OutputFile)
  635.     return FALSE;
  636.  
  637. #ifndef NO_GUI
  638.   if (isInteractive)
  639.   {
  640.     char buf[300];
  641.     sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(InputFile));
  642.     frame->SetTitle(buf);
  643.   }
  644.  
  645.   wxStartTimer();
  646. #endif
  647.  
  648.   // Find extension-less filename
  649.   strcpy(FileRoot, OutputFile);
  650.   StripExtension(FileRoot);
  651.   sprintf(ContentsName, "%s.con", FileRoot);
  652.   sprintf(TmpContentsName, "%s.cn1", FileRoot);
  653.   sprintf(RefName, "%s.ref", FileRoot);
  654.  
  655.   TexPathList.EnsureFileAccessible(InputFile);
  656.   if (!bulletFile)
  657.   {
  658.     char *s = TexPathList.FindValidPath("bullet.bmp");
  659.     if (s)
  660.       bulletFile = copystring(FileNameFromPath(s));
  661.   }
  662.   if (FileExists(RefName))
  663.     ReadTexReferences(RefName);
  664.     
  665.   Bool success = FALSE;
  666.  
  667.   if (InputFile && OutputFile)
  668.   {
  669.     if (!FileExists(InputFile))
  670.     {
  671.       OnError("Cannot open input file!");
  672.       TexCleanUp();
  673.       return FALSE;
  674.     }
  675. #ifndef NO_GUI
  676.     if (isInteractive)
  677.     {
  678.       char buf[50];
  679.       sprintf(buf, "Working, pass %d...", passNumber);
  680.       frame->SetStatusText(buf);
  681.     }
  682. #endif
  683.     OkToClose = FALSE;
  684.     OnInform("Reading LaTeX file...");
  685.     TexLoadFile(InputFile);
  686.  
  687.     switch (convertMode)
  688.     {
  689.       case TEX_RTF:
  690.       {
  691.         success = RTFGo();
  692.         break;
  693.       }
  694.       case TEX_XLP:
  695.       {
  696.         success = XLPGo();
  697.         break;
  698.       }
  699.       case TEX_HTML:
  700.       {
  701.         success = HTMLGo();
  702.         break;
  703.       }
  704.     }
  705.   }
  706.   if (stopRunning)
  707.   {
  708.     OnInform("*** Aborted by user.");
  709.     success = FALSE;
  710.     stopRunning = FALSE;
  711.   }
  712.  
  713.   if (success)
  714.   {
  715.     WriteTexReferences(RefName);
  716.     TexCleanUp();
  717.     startedSections = FALSE;
  718.  
  719.     char buf[100];
  720. #ifndef NO_GUI
  721.     long tim = wxGetElapsedTime();
  722.     sprintf(buf, "Finished in %ld seconds.", (long)(tim/1000.0));
  723.     OnInform(buf);
  724.     if (isInteractive)
  725.     {
  726.       sprintf(buf, "Done, %d %s.", passNumber, (passNumber > 1) ? "passes" : "pass");
  727.       frame->SetStatusText(buf);
  728.     }
  729. #else
  730.     sprintf(buf, "Done, %d %s.", passNumber, (passNumber > 1) ? "passes" : "pass");
  731.     OnInform(buf);
  732. #endif
  733.     passNumber ++;
  734.     OkToClose = TRUE;
  735.     return TRUE;
  736.   }
  737.  
  738.   TexCleanUp();
  739.   startedSections = FALSE;
  740.  
  741.   OnInform("Sorry, unsuccessful.");
  742.   OkToClose = TRUE;
  743.   return FALSE;
  744. }
  745.  
  746. void OnError(char *msg)
  747. {
  748. #ifdef NO_GUI
  749.   cerr << "Error: " << msg << "\n";
  750.   cerr.flush();
  751. #else
  752.   if (isInteractive)
  753.     (*frame->textWindow) << "Error: " << msg << "\n";
  754.   else
  755. #ifdef wx_x
  756.   {
  757.     cerr << "Error: " << msg << "\n";
  758.     cerr.flush();
  759.   }
  760. #endif
  761. #ifdef wx_msw
  762.     wxError(msg);
  763. #endif
  764.   Tex2RTFYield(TRUE);
  765. #endif // NO_GUI
  766. }
  767.  
  768. void OnInform(char *msg)
  769. {
  770. #ifdef NO_GUI
  771.   cout << msg << "\n";
  772.   cout.flush();
  773. #else
  774.   if (isInteractive)
  775.     (*frame->textWindow) << msg << "\n";
  776.   else
  777. #ifdef wx_x
  778.   {
  779.     cout << msg << "\n";
  780.     cout.flush();
  781.   }
  782. #endif
  783. #ifdef wx_msw
  784.     {}
  785. #endif
  786.   if (isInteractive)
  787.   {
  788.     Tex2RTFYield(TRUE);
  789.   }
  790. #endif // NO_GUI
  791. }
  792.  
  793. void OnMacro(int macroId, int no_args, Bool start)
  794. {
  795.   switch (convertMode)
  796.   {
  797.     case TEX_RTF:
  798.     {
  799.       RTFOnMacro(macroId, no_args, start);
  800.       break;
  801.     }
  802.     case TEX_XLP:
  803.     {
  804.       XLPOnMacro(macroId, no_args, start);
  805.       break;
  806.     }
  807.     case TEX_HTML:
  808.     {
  809.       HTMLOnMacro(macroId, no_args, start);
  810.       break;
  811.     }
  812.   }
  813. }
  814.  
  815. Bool OnArgument(int macroId, int arg_no, Bool start)
  816. {
  817.   switch (convertMode)
  818.   {
  819.     case TEX_RTF:
  820.     {
  821.       return RTFOnArgument(macroId, arg_no, start);
  822.       break;
  823.     }
  824.     case TEX_XLP:
  825.     {
  826.       return XLPOnArgument(macroId, arg_no, start);
  827.       break;
  828.     }
  829.     case TEX_HTML:
  830.     {
  831.       return HTMLOnArgument(macroId, arg_no, start);
  832.       break;
  833.     }
  834.   }
  835.   return TRUE;
  836. }
  837.  
  838. /*
  839.  * DDE Stuff
  840.  */
  841. #ifdef wx_msw
  842.  
  843. /*
  844.  * Server
  845.  */
  846.  
  847. wxConnection *Tex2RTFServer::OnAcceptConnection(char *topic)
  848. {
  849.   if (strcmp(topic, "TEX2RTF") == 0)
  850.   {
  851.     if (!ipc_buffer)
  852.       ipc_buffer = new char[1000];
  853.       
  854.     return new Tex2RTFConnection(ipc_buffer, 4000);
  855.   }
  856.   else
  857.     return NULL;
  858. }
  859.  
  860.  /*
  861.   * Connection
  862.   */
  863.   
  864. Tex2RTFConnection::Tex2RTFConnection(char *buf, int size):wxConnection(buf, size)
  865. {
  866. }
  867.  
  868. Tex2RTFConnection::~Tex2RTFConnection(void)
  869. {
  870. }
  871.  
  872. Bool SplitCommand(char *data, char *firstArg, char *secondArg)
  873. {
  874.   firstArg[0] = 0;
  875.   secondArg[0] = 0;
  876.   int i = 0;
  877.   int len = strlen(data);
  878.   Bool stop = FALSE;
  879.   // Find first argument (command name)
  880.   while (!stop)
  881.   {
  882.     if (data[i] == ' ' || data[i] == 0)
  883.       stop = TRUE;
  884.     else
  885.     {
  886.       firstArg[i] = data[i];
  887.       i ++;
  888.     }
  889.   }
  890.   firstArg[i] = 0;
  891.   if (data[i] == ' ')
  892.   {
  893.     // Find second argument
  894.     i ++;
  895.     int j = 0;
  896.     while (data[i] != 0)
  897.     {
  898.       secondArg[j] = data[i];
  899.       i ++;
  900.       j ++;
  901.     }
  902.     secondArg[j] = 0;
  903.   }
  904.   return TRUE;
  905. }
  906.  
  907. Bool Tex2RTFConnection::OnExecute(char *topic, char *data, int size, int format)
  908. {
  909.   strcpy(Tex2RTFLastStatus, "OK");
  910.  
  911.   char firstArg[50];
  912.   char secondArg[300];
  913.   if (SplitCommand(data, firstArg, secondArg))
  914.   {
  915.     Bool hasArg = (strlen(secondArg) > 0);
  916.     if (strcmp(firstArg, "INPUT") == 0 && hasArg)
  917.     {
  918.       if (InputFile) delete[] InputFile;
  919.       InputFile = copystring(secondArg);
  920.       if (frame)
  921.       {
  922.         char buf[100];
  923.         sprintf(buf, "Tex2RTF [%s]", FileNameFromPath(InputFile));
  924.         frame->SetTitle(buf);
  925.       }
  926.     }
  927.     else if (strcmp(firstArg, "OUTPUT") == 0 && hasArg)
  928.     {
  929.       if (OutputFile) delete[] OutputFile;
  930.       OutputFile = copystring(secondArg);
  931.     }
  932.     else if (strcmp(firstArg, "GO") == 0)
  933.     {
  934.       strcpy(Tex2RTFLastStatus, "WORKING");
  935.       if (!Go())
  936.         strcpy(Tex2RTFLastStatus, "CONVERSION ERROR");
  937.       else
  938.         strcpy(Tex2RTFLastStatus, "OK");
  939.     }
  940.     else if (strcmp(firstArg, "EXIT") == 0)
  941.     {
  942.       if (frame && frame->OnClose())
  943.         delete frame;
  944.     }
  945.     else if (strcmp(firstArg, "MINIMIZE") == 0 || strcmp(firstArg, "ICONIZE") == 0)
  946.     {
  947.       if (frame)
  948.         frame->Iconize(TRUE);
  949.     }
  950.     else if (strcmp(firstArg, "SHOW") == 0 || strcmp(firstArg, "RESTORE") == 0)
  951.     {
  952.       if (frame)
  953.       {
  954.         frame->Iconize(FALSE);
  955.         frame->Show(TRUE);
  956.       }
  957.     }
  958.     else
  959.     {
  960.       // Try for a setting
  961.       strcpy(Tex2RTFLastStatus, RegisterSetting(firstArg, secondArg, FALSE));
  962. #ifndef NO_GUI
  963.       if (frame && strcmp(firstArg, "conversionMode") == 0)
  964.       {
  965.         char buf[100];
  966.         strcpy(buf, "In ");
  967.  
  968.         if (winHelp && (convertMode == TEX_RTF))
  969.           strcat(buf, "WinHelp RTF");
  970.         else if (!winHelp && (convertMode == TEX_RTF))
  971.           strcat(buf, "linear RTF");
  972.         else if (convertMode == TEX_HTML) strcat(buf, "HTML");
  973.         else if (convertMode == TEX_XLP) strcat(buf, "XLP");
  974.           strcat(buf, " mode.");
  975.         frame->SetStatusText(buf, 1);
  976.       }
  977. #endif
  978.     }
  979.   }
  980.   return TRUE;
  981. }
  982.  
  983. char *Tex2RTFConnection::OnRequest(char *topic, char *item, int *size, int format)
  984. {
  985.   return Tex2RTFLastStatus;
  986. }
  987.  
  988. #endif
  989.